package utils.db;

import javax.persistence.EntityManager;
import javax.persistence.FlushModeType;
import javax.persistence.PersistenceException;

import play.db.DB;
import play.db.jpa.JPA;
import play.exceptions.JPAException;

public class TransactionUtil {

	/**
	 * 开启一个事务
	 * 
	 * 2015年10月5日 下午4:54:25
	 * 
	 * @auth huan208050@vip.qq.com
	 */
	public static void startTx() {
		// Logger.debug("startTx for Thread: %s and waiting for new row insert
		// for cds_conversion_queue ... ",
		// Thread.currentThread());
		if (JPA.local.get() != null) {
			try {
				JPA.local.get().entityManager.close();
			} catch (Exception e) {
			}
			JPA.local.remove();
		}
		JPA jpa = new JPA();
		jpa.entityManager = JPA.entityManagerFactory.createEntityManager();
		jpa.entityManager.setFlushMode(FlushModeType.AUTO);
		jpa.entityManager.getTransaction().begin();
		JPA.local.set(jpa);
	}

	/**
	 * 关闭一个事务
	 * 
	 * @param rollback
	 * @param ignoreQueue
	 *            2015年10月5日 下午4:52:44
	 * @auth huan208050@vip.qq.com
	 */
	private static void closeTx(boolean rollback, boolean ignoreQueue) {
		// Logger.debug("closeTx(%s) for Thread: %s", rollback,
		// Thread.currentThread());
		if (JPA.local.get() == null) {
			return;
		}
		EntityManager manager = JPA.local.get().entityManager;
		try {
			DB.getConnection().setAutoCommit(false);
		} catch (Exception e) {
			play.Logger.error(e, "Why the driver complains here?", new Object[0]);
		}

		if (!ignoreQueue) {
			manager.createNativeQuery("select queue_end()").getResultList();
		}

		if (manager.getTransaction().isActive())
			if ((rollback) || manager.getTransaction().getRollbackOnly())
				manager.getTransaction().rollback();
			else
				try {
					manager.getTransaction().commit();
				} catch (Throwable e) {
					for (int i = 0; i < 10; ++i) {
						if ((e instanceof PersistenceException) && (e.getCause() != null)) {
							e = e.getCause();
							break;
						}
						e = e.getCause();
						if (e == null) {
							break;
						}
					}
					throw new JPAException("Cannot commit", e);
				}
	}

	/**
	 * 关闭事务
	 * 
	 * @param rollback
	 *            是否回滚 true，回滚，false，不回滚 2015年10月5日 下午4:56:43
	 * @auth huan208050@vip.qq.com
	 */
	public static void closeTx(boolean rollback) {
		closeTx(rollback, true);
	}

}
